

#include <QListWidgetItem>
#include <QListWidget>
#include <QInputDialog>
#include <math.h>
#include <QMessageBox>
#include "mainwindow.h"
#include "ui_mainwindow.h"



extern "C"
{


extern int lmeklet(char * rez,char * poz) asm("lmeklet");

}


#ifndef GAJIENU_IMPL_H
#define GAJIENU_IMPL_H


const  int client::gajienu_impl::pagriezieni[][24]={
{3,0,1,2,4,5,6,7,17,16,10,11,21,20,14,15,12,13,18,19,8,9,22,23},
{0,1,2,3,5,6,7,4,8,9,22,23,12,13,18,19,16,17,11,10,20,21,15,14},
{0,21,22,3,4,17,18,7,11,8,9,10,12,13,14,15,16,2,1,19,20,6,5,23},
{19,1,2,16,23,5,6,20,8,9,10,11,13,14,15,12,4,17,18,7,0,21,22,3},
{9,10,2,3,13,14,6,7,8,5,4,11,12,1,0,15,17,18,19,16,20,21,22,23},
{0,1,12,15,4,5,8,11,3,9,10,2,7,13,14,6,16,17,18,19,23,20,21,22}
};

#endif // CUBEGRAMMAR_H


    void MyThread::run()
 {
    //exec();
        cuberesult = lmeklet(rez,poz);



 }


    void MainWindow::gajpr(int gaj)
    {

          int iepr = ui->mykubs->gajsk;
if(gaj<24)
        {

if(ui->mykubs->gajsk > 0 && abs(gaj - ui->mykubs->gajieni[ui->mykubs->gajsk-1]) == 6)
{

ui->mykubs->gajsk--;
attgajienus(iepr);
}
else
{
if(iepr <15)
    {
ui->mykubs->gajieni[iepr] = gaj;
ui->mykubs->gajsk++;
attgajienus(iepr);
}

}


}else {
    if(gaj == 25)attgajienus(iepr);
}
}



void MainWindow::gajatp()
{
if(ui->mykubs->gajsk > 0)
    {
ui->mykubs->pagriezt(ui->mykubs->gajieni[ui->mykubs->gajsk-1] > 5 ? ui->mykubs->gajieni[ui->mykubs->gajsk-1] - 6 : ui->mykubs->gajieni[ui->mykubs->gajsk-1] + 6);


    }


}



void MainWindow::labpoz()
    {


if(ui->stavlist->selectedItems().count() > 0 )
    {
    int knum = ui->stavlist->row(ui->stavlist->selectedItems().first());

 (*mypozicijas)[knum].gajsk=ui->mykubs->gajsk ;
for(int i = 0 ; i< ui->mykubs->gajsk;i++)(*mypozicijas)[knum].gajieni[i]=ui->mykubs->gajieni[i]  ;
for(int i = 0 ; i!= 24;i++) (*mypozicijas)[knum].pozdata[i]=ui->mykubs->vietas[i] ;
}
}


void MainWindow::delpoz()
{
if(ui->stavlist->selectedItems().count() > 0 )
    {
      int knum = ui->stavlist->row(ui->stavlist->selectedItems().first());

       QListWidgetItem* item = ui->stavlist->selectedItems().first();
      delete item;

    mypozicijas->removeAt(knum);

}
}

    void MainWindow::getpoz()
    {
 bool ok;
     QString text = QInputDialog::getText(this, trUtf8("Kuba stāvoklis"),
                                          trUtf8("St. Nosaukums"), QLineEdit::Normal,
                                          QString(), &ok);
      if (ok && !text.isEmpty())
     {

        pozicija * npos = new pozicija();
npos->gajsk = ui->mykubs->gajsk;
for(int i = 0 ; i!= ui->mykubs->gajsk;i++)npos->gajieni[i] = ui->mykubs->gajieni[i];
npos->nosaukums = text;
for(int i = 0 ; i!= 24;i++)npos->pozdata[i] = ui->mykubs->vietas[i];
mypozicijas->append(*npos);
QListWidgetItem * witem =  new QListWidgetItem ( text );
ui->stavlist->addItem(witem);

}

    }


   void MainWindow::setpoz( QListWidgetItem * item)
   {
int knum = ui->stavlist->row(item);
//item->i
int ieprgaj = ui->mykubs->gajsk;
ui->mykubs->gajsk = mypozicijas->at(knum).gajsk;
for(int i = 0 ; i!= ui->mykubs->gajsk;i++)ui->mykubs->gajieni[i] = mypozicijas->at(knum).gajieni[i];
attgajienus(ieprgaj);
for(int i = 0 ; i!= 24;i++) ui->mykubs->vietas[i]=mypozicijas->at(knum).pozdata[i] ;
updtab();
ui->mykubs->updateGL();
   }



   void MainWindow::attgajienus(int ieprg)
   {
   for(int pagsk = 0;pagsk < ui->mykubs->gajsk;pagsk++)

ui->pagrt->item(0,pagsk)->setText(QString("%1").arg((int)ui->mykubs->gajieni[pagsk]));

if(ieprg >  ui->mykubs->gajsk)
    for(int pagsk = ui->mykubs->gajsk;pagsk < ieprg;pagsk++)
  ui->pagrt->item(0,pagsk)->setText("");



   }



  void MainWindow::salikt()
    {

 int ieprg =  ui->mykubs->gajsk;
ui->mykubs->meklet();
attgajienus(ieprg);



}


void MainWindow::updtab()

{
for(int cols = 0;cols<8;cols++)
    for(int rows = 0;rows <3;rows++)
        ui->stavt->item(rows,cols)->setText(QString("%1").arg((int)ui->mykubs->vietas[cols + rows *8]));
ui->mykubs-> atrsturus();
for(int cols = 0;cols<8;cols++)
        ui->ststavt->item(0,cols)->setText(QString("%1").arg((int)ui->mykubs->vsturi[cols]));

}

void MainWindow::savetofiles()
{
    mypozicijas->savetofile();
}


void MainWindow::readfromfiles()
{
   int oldcount = mypozicijas->count();
    mypozicijas->loadfromfile();
for(int i= oldcount;i<mypozicijas->count();i++)ui->stavlist->addItem(new QListWidgetItem ( mypozicijas->at(i).nosaukums ));

}

void MainWindow::simpbs()
{
   ui->mykubs->simgr(ui->spinBox->value());
     updtab();


     ui->mykubs->updateGL();

}

void MainWindow::simbs()
{
    ui->mykubs->invert();
     updtab();
    for(int pagsk = 0;pagsk < ui->mykubs->gajsk;pagsk++)ui->mykubs->gajieni[pagsk] = ui->mykubs->gajieni[pagsk] > 5 ? ui->mykubs->gajieni[pagsk] - 6 : ui->mykubs->gajieni[pagsk] + 6;
 attgajienus(ui->mykubs->gajsk);
     ui->mykubs->updateGL();
}






MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    for(int cols = 0;cols<8;cols++)
    for(int rows = 0;rows <3;rows++)
        if(ui->stavt->item(rows,cols)==0)
ui->stavt->setItem(rows,cols,new QTableWidgetItem(""));
    for(int cols = 0;cols<14;cols++)
        ui->pagrt->setItem(0,cols,new QTableWidgetItem(""));
      for(int cols = 0;cols<8;cols++)
        ui->ststavt->setItem(0,cols,new QTableWidgetItem(""));

ui->stavlist->addAction(ui->actiondzpoz);
ui->stavlist->addAction(ui->actionkomb);
ui->stavlist->addAction(ui->actionsalidz);
   timeline= new QTimer();
   gradient = 10;

mypozicijas = new pozicijas();
calc = new calculator(*mypozicijas);
thread = new MyThread();

   ui->mykubs->sakartot();

 QObject::connect( timeline, SIGNAL(timeout()),this, SLOT(timeticked()));
 QObject::connect( thread, SIGNAL(finished()),this, SLOT(beidzis()));

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::saktmeklet()
{
   if(thread->isRunning())thread->terminate();
   else
   {
for (int i = 0;i!=24;i++)thread->poz[i]= ui->mykubs->vietas[i];
timerticks = 0;
ui->pulkstl->setText(QString("00:00:00"));
gradient=10;
ui->meklind->setInvertedAppearance(false);
timeline->start(200);
thread->start();
}
}

      void MainWindow::beidzis()
{
  timeline->stop();
  ui->meklind->setValue(0);
for(int i = 0;i!=thread->cuberesult+1;i++)
{
 int gaj = thread->rez[thread->cuberesult-i]/3;
 gaj = gaj > 5 ? gaj -6 : gaj + 6;
   ui->mykubs->gajieni[i]=gaj ;
}

QMessageBox::information ( this, trUtf8("Meklēšana pabeigta!") ,
                           trUtf8("Atrasts ar %1 gājieniem").arg(thread->cuberesult+1)) ;
int ieprg = ui->mykubs->gajsk;
ui->mykubs->gajsk=thread->cuberesult+1;
attgajienus(ieprg);

}


       void MainWindow::timeticked()
{
timerticks++;
int seconds = floor(timerticks/5);
int minutes = floor(seconds / 60);
seconds -= 60*minutes;
int hours = floor(minutes/60);
minutes -= 60*hours;
ui->pulkstl->setText(QString("").sprintf("%02d:%02d:%02d",hours ,minutes ,seconds ));

 ui->meklind->setValue(ui->meklind->value() + gradient);
if(ui->meklind->value() == 100 ||  ui->meklind->value() == 0 )
           {
gradient = -gradient;
ui->meklind->setInvertedAppearance(!ui->meklind->invertedAppearance());
}


}





void MainWindow::on_actionkomb_triggered()
{
    if(ui->stavlist->selectedItems().count() > 0 )
    {
      int knum = ui->stavlist->row(ui->stavlist->selectedItems().first());
   ui->mykubs->kombinet((*mypozicijas)[knum].pozdata);

}


    //
}

void MainWindow::on_actionsalidz_triggered()
{
   if(ui->stavlist->selectedItems().count() > 0 )
    {
      int knum = ui->stavlist->row(ui->stavlist->selectedItems().first());
int sal=   ui->mykubs->salidz((*mypozicijas)[knum].pozdata);
if(sal) QMessageBox::information ( this, trUtf8("Rezultāts") ,
                           trUtf8("Netakrīt ar starpību %1!").arg(sal)) ;
else QMessageBox::information ( this, trUtf8("Rezultāts") ,
                                                       trUtf8("Sakrīt!")) ;
}
}

void MainWindow::on_formb_clicked()
{
       using boost::spirit::ascii::space;

 if (ui->forme->text().isEmpty())QMessageBox::information ( this, trUtf8("Atgādinājums") ,
                           trUtf8("Ieraksti formulas tekstu!")) ;

     else{
      std::string str = ui->forme->text().toStdString();

     int result[24];
int * resptr = result;
        std::string::const_iterator iter = str.begin();
        std::string::const_iterator end = str.end();
        bool r = phrase_parse(iter, end, *calc, space, resptr);

        if (r && iter == end)
        {

           ui->mykubs->kombinet(resptr);
            std::cout << "-------------------------\n|";
            std::cout << "Parsing succeeded\n";
            for(int i = 0;i<24;i++)

            std::cout <<  resptr[i] << "|" ;

            std::cout << "\n-------------------------\n";
        }
        else
        {

            QMessageBox::information ( this, trUtf8("Rezultāts") ,
                           trUtf8("Formulas kļūda!!!")) ;
            std::cout << "-------------------------\n";
            std::cout << "Parsing failed\n";
            std::cout << "-------------------------\n";
        }

    }
//

}



